package com.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.pojo.AddressBook;
import com.reggie.service.IAddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;


    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook){
        log.info("添加地址addressBook:{}",addressBook);
        //获取当前登录的id
        Long userId = BaseContext.getCurrentId();
        //为传过来的addressBook添加属性userId
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);

        return R.success("保存地址成功");
    }

    @PutMapping("/default")
    public R<String> setDefaultAddressBook(@RequestBody AddressBook addressBook){
        log.info("设置默认地址：{}",addressBook);
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        //获取当前登录的id
        Long userId = BaseContext.getCurrentId();
        //1.将当前用户所有地址设置为非默认地址：0不是默认地址,1是默认地址
        //update address_book set is_default = 0 where  user_id =  ? userId
        luw.set(AddressBook::getIsDefault,0);
        luw.eq(AddressBook::getUserId,userId);
        //2.根据传过来地址中的id将此条数据设置为默认地址
        //update address_book set is_default = 1 where  id =  ? addressBook.getId()
        luw.set(AddressBook::getIsDefault,1);
        luw.eq(AddressBook::getId,addressBook.getId());

        addressBookService.update(luw);
        return R.success("设置成功");
    }

    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询地址:{}",id);
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> getByDefault(){
        log.info("根据id和默认查询地址");
        //获取当前登录的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        lqw.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(lqw);
        return R.success(addressBook);
    }

    @GetMapping("/list")
    public R<List<AddressBook>> getAddressBookList(){
        log.info("根据登录的用户id查询他的所有地址");
        //获取当前登录的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    @DeleteMapping
    public R<String> deleteAddressBooks(@RequestParam List<Long> ids){
        log.info("根据ids删除地址:{}",ids);
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
